package com.ydj;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;

public class ModifyRuoyiVuePlus {
    static String dirName="xingfa";
    static String projectName="xingfa-base";
    static String packageName="com.xingfa";
    static String artificatName="xingfa";
    static String groupName="com.xingfa";
    static String siteName="兴发基础件";
    static String author ="xf109633";
    
    static Map<String,String> replaceMap=new LinkedHashMap<>();
    static {
        replaceMap.put("ruoyi-vue-plus", projectName);
        replaceMap.put("com.ruoyi", packageName);
        replaceMap.put("ruoyi-", artificatName+"-");
        replaceMap.put("/ruoyi/", "/xingfa/");
        replaceMap.put("@author ruoyi", "@author xf109633");
        replaceMap.put("@author Lion Li", "@author xf109633");
        replaceMap.put("RuoYi-Vue-Plus启动成功","兴发基础框架启动成功");
        replaceMap.put("RuoYi-Vue-Plus","XingfaBase");
        replaceMap.put("RuoYi", "XinfaBase");
        replaceMap.put("ruoyi.name", "xingfabase.name");
        replaceMap.put("ruoyi:", "xingfabase:");
        replaceMap.put("prefix = \"ruoyi\"", "prefix = \"xingfabase\"");
        replaceMap.put("ruoyiConfig", "xingfaBaseConfig");
        replaceMap.put("ruoYiConfig", "xingfaBaseConfig");
        replaceMap.put("name: ruoyi", "name: xingfaBase");
        
        
    }
    static List<String> excludePath=new ArrayList<>();
    static {
        excludePath.add("node_modules");
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String root="D:\\git\\RuoYi-Vue-Plus";
        File f=new File(root);
        changeFilename(f);
    }

    private static void changeFilename(File f) {
        String fileName=f.getName();
        if(excludePath.size()>0) {
            for(String tmpPath:excludePath) {
                if(fileName.indexOf(tmpPath)>-1) {
                    return;
                }
            }
        }
        if(fileName.indexOf("ruoyi-")>-1) {
            File tmpFile=new File(f.getParentFile().getPath()+File.separator+f.getName().replace("ruoyi-", dirName+"-"));
            try {
                if(f.isDirectory()) {
                    FileUtils.copyDirectory(f, tmpFile);
                    FileUtils.deleteDirectory(f);
                    f=tmpFile;
                }else {
                    FileUtils.copyFile(f, tmpFile);
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        if(f.isDirectory()) {
            for(File ff: f.listFiles()) {
                changeFilename(ff);
            }
            System.out.println(f.getPath());
            if(f.getPath().endsWith("com\\ruoyi")) {
                File tmpFile=new File(f.getParentFile().getParentFile().getPath()+"\\com\\xingfa");
                
//                Boolean b=f.renameTo(tmpFile);
                try {
                    FileUtils.copyDirectory(f.getParentFile(), tmpFile.getParentFile());
                    FileUtils.deleteDirectory(f.getParentFile());
//                    FileUtils.copyDirectoryToDirectory(f.getParentFile(), tmpFile.getParentFile());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    FileUtils.copyDirectory(new File(tmpFile.getParentFile().getPath()+"\\ruoyi"), new File(tmpFile.getParentFile()+"\\xingfa"));
                    FileUtils.deleteDirectory(new File(tmpFile.getParentFile().getPath()+"\\ruoyi"));
                } catch(Exception e) {
                    e.printStackTrace();
                }
                f=tmpFile;
            }
        }else {
            // 打开文件替换里面的 内容
            replaceContent(f.getPath(),replaceMap);
        }
        
//        File[] files=f.listFiles();
//        for(file)
//
        
    }

    private static void replaceContent(String filePath, Map<String,String> replaceMap) {
        try {
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
            StringBuilder content = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                for(String key:replaceMap.keySet()) {
                    line=line.replaceAll(key, replaceMap.get(key));
                }
                content.append(line).append(System.lineSeparator());
            }
            reader.close();

            FileWriter writer = new FileWriter(filePath);
            writer.write(content.toString());
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    private static void copyPath(String src,String dest) {
        
    }
}
